package com.fare.taluo.application.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.fare.taluo.application.AnalyseQueryApplication;
import com.fare.taluo.base.ddd.application.BaseApplicationImpl;
import com.fare.taluo.vo.core.AgentInfo;
import com.fare.taluo.vo.core.PlaneInfo;

@Scope("singleton")
@Service("analyseQueryApplication")
public class AnalyseQueryApplicationImpl extends BaseApplicationImpl implements
		AnalyseQueryApplication {
	/**
	 * 先将List<PlaneInfo> 转存为map(航班号,List<String>[price,price] )
	 * 最后List<String>[price,price] 只保留最低价。List<String>[price]
	 */
	public HashMap<String,PlaneInfo> caluteHangbanLowPrice(List<PlaneInfo>  infolist){		 
		HashMap<String,List<PlaneInfo>> mapprice = new HashMap<String,List<PlaneInfo>>();
		HashMap<String,PlaneInfo> mapprice2 = new HashMap<String, PlaneInfo>();
		for(PlaneInfo plane:infolist){
			if(!StringUtils.isBlank(plane.getFlightId())&&!StringUtils.isBlank(plane.getLowprPrice())&&!mapprice.containsKey(plane.getFlightId())){
				List<PlaneInfo> strlist = new ArrayList<PlaneInfo>();
				mapprice.put(plane.getFlightId(), strlist);
			}	
			    if(!StringUtils.isBlank(plane.getLowprPrice())){
			 	mapprice.get(plane.getFlightId()).add(plane);
			////	mapprice.get(plane.getFlightId()).add(plane.getGrapDate());
			    }
			}
		if(mapprice!=null&&mapprice.size()>0){
			for( String str :mapprice.keySet()){
				List<PlaneInfo> plist = mapprice.get(str);
				Double price =Double.parseDouble(plist.get(0).getLowerPrice());
				PlaneInfo info=null;
				if(plist.size()>0){
				for(int i=0;i<plist.size();i++){
					Double lowprice = Double.parseDouble(plist.get(i).getLowerPrice());
					if(lowprice<=price){
						price = lowprice;
						info=plist.get(i);
					  }
				  }
				}
				if(info!=null){
					mapprice2.put(str, info);
				}
			}
			
		}
		return mapprice2;
	}
	public HashMap<String,String> caluteAlllowPrice(List<PlaneInfo>  infolist){	
		HashMap<String,String> flghtinfo = new HashMap<String, String>();
		Integer price =500000;
		String  flightno= "";
		for(PlaneInfo plane:infolist){
			if(!StringUtils.isBlank(plane.getFlightId())&&!StringUtils.isBlank(plane.getLowprPrice())){	
				Integer lowprice = Integer.parseInt(plane.getLowprPrice());
				if(lowprice<price){
					flightno = plane.getFlightId();
					price = lowprice;
				}
				else if(lowprice.equals(price)){
					flightno += ","+plane.getFlightId();
					price = lowprice;
				}
			} 
		}
		if(!StringUtils.isBlank(flightno)){
			flghtinfo.put(flightno, price.toString());
		}
		return flghtinfo;
	}
	
	
	/**
	 * 从代理商中找出最低价并把最低价的代理商放到代理商列表第一位
	 * 
	 * @param agentMap
	 * @param planeInfo
	 * @return
	 */
	public PlaneInfo sortAgentInfo(Map<String, AgentInfo> agentMap,
			PlaneInfo planeInfo) {
		// 非官网渠道:从代理商中找出最低价并把最低价的代理商放到代理商列表第一位
		if (agentMap.keySet().size() != 0) { // 若有AgentInfo,则为非官网渠道
			Integer lowestPriceOfFlight = Integer.valueOf(agentMap.get("1")
					.getPrice());
			for (String key : agentMap.keySet()) {
				if (agentMap.get(key).getPrice() == null)
					continue;
				Integer agentPrice = Integer.valueOf(agentMap.get(key)
						.getPrice());
				AgentInfo agentInfo = agentMap.get(key);
				if (lowestPriceOfFlight >= agentPrice) {
					lowestPriceOfFlight = agentPrice;
					planeInfo.getAgentInfos().add(0, agentInfo);
				} else {
					planeInfo.getAgentInfos().add(agentInfo);
				}
			}
		}
		return planeInfo;
	}
	
	
}
